﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECare.BUS.Data;
using ECare.Database;

namespace ECare.BUS.Workers
{
    public class ComputeResourceInProducingWorker : BackgroundWorker
    {

        public plan_detail_step_info Currentstep { get; set; }

        public ComputeResourceInProducingWorker()
        {
            this.DoWork += ComputeResourceInProducingWorker_DoWork;
        }

        void ComputeResourceInProducingWorker_DoWork(object sender, DoWorkEventArgs e)
        {

            using (var context = new Entities())
            {
                var findplanstep = context.plan_detail_step.FirstOrDefault(a => a.plan_detail_step_id == this.Currentstep.plan_detail_step_id);
                if (findplanstep == null)
                {
                    //can not find this step.
                    e.Result = false;
                    return;
                }

                this.Currentstep.plan_detail_end_date = DateTime.Now;
                findplanstep.plan_detail_end_date = DateTime.Now;//完成时间 
                findplanstep.plan_detail_step_status = 1;//finished.
                findplanstep.plan_detail_step_user_id = ApplicationData.CurrentUserInfo.UserId;//当前操作人...

                context.SaveChanges();

                e.Result = true;
                return;

                //var resources = context.sp_getResourceInProducing(Currentstep.resource_id).ToList();

                //var currentneed = Currentstep.resource_need;
                //foreach (var resource in resources)
                //{
                //    var left = (resource.supply_unit_kg - resource.usage).Value;

                //    if (left < currentneed)
                //    {
                //        //add the use record.
                //        //value==left.
                //        var newusage = new supply_detail_usage();
                //        newusage.plan_detail_step_id = Currentstep.plan_detail_step_id;
                //        newusage.supply_detail_id = resource.supply_detail_id;
                //        newusage.supply_detail_usage_isactive = true;
                //        newusage.usage_create_by = 0;
                //        newusage.usage_create_time = DateTime.Now;
                //        newusage.usage_number = left;

                //        context.supply_detail_usage.Add(newusage);
                //        currentneed = currentneed - left;
                //        if (currentneed == 0)
                //            break;
                //    }
                //    else
                //    {
                //        //add the use record
                //        //value=current need.
                //        var newusage = new supply_detail_usage();
                //        newusage.plan_detail_step_id = Currentstep.plan_detail_step_id;
                //        newusage.supply_detail_id = resource.supply_detail_id;
                //        newusage.supply_detail_usage_isactive = true;
                //        newusage.usage_create_by = 0;
                //        newusage.usage_create_time = DateTime.Now;
                //        newusage.usage_number = currentneed;

                //        context.supply_detail_usage.Add(newusage);
                //        break;
                //    }

                //}
                //context.SaveChanges();
                //e.Result = true;

            }

        }


    }
}
